package com.sub.project.mall.contorller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.sub.common.utils.ServletUtils;
import com.sub.common.utils.StringUtils;
import com.sub.common.utils.poi.ExcelUtil;
import com.sub.framework.aspectj.lang.annotation.Log;
import com.sub.framework.aspectj.lang.enums.BusinessType;
import com.sub.framework.security.LoginUser;
import com.sub.framework.security.service.TokenService;
import com.sub.framework.web.controller.BaseController;
import com.sub.framework.web.domain.AjaxResult;
import com.sub.framework.web.page.TableDataInfo;
import com.sub.project.mall.domain.PmsProductCategory;
import com.sub.project.mall.service.IPmsProductCategoryService;
import com.sub.project.system.domain.SysDept;
import com.sun.security.auth.UserPrincipal;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-08-03
 */
@RestController
@RequestMapping("/mall/category" )
public class PmsProductCategoryController extends BaseController {
    @Autowired
    private IPmsProductCategoryService pmsProductCategoryService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('mall:category:list')" )
    @GetMapping("/list" )
    public AjaxResult list(PmsProductCategory pmsProductCategory) {
        startPage();
        QueryWrapper<PmsProductCategory> queryWrapper = new QueryWrapper<PmsProductCategory>();
if(pmsProductCategory.getParentId() != null){
     queryWrapper.eq("parent_id" ,pmsProductCategory.getParentId());
}
                        if (StringUtils.isNotBlank(pmsProductCategory.getName())){
            queryWrapper.like("name" ,pmsProductCategory.getName());
        }
            if(pmsProductCategory.getLevel() != null){
     queryWrapper.eq("level" ,pmsProductCategory.getLevel());
}
    if(pmsProductCategory.getProductCount() != null){
     queryWrapper.eq("product_count" ,pmsProductCategory.getProductCount());
}
    if(StringUtils.isNotBlank(pmsProductCategory.getProductUnit())){
     queryWrapper.eq("product_unit" ,pmsProductCategory.getProductUnit());
}
    if(pmsProductCategory.getNavStatus() != null){
     queryWrapper.eq("nav_status" ,pmsProductCategory.getNavStatus());
}
    if(pmsProductCategory.getShowStatus() != null){
     queryWrapper.eq("show_status" ,pmsProductCategory.getShowStatus());
}
    if(pmsProductCategory.getSort() != null){
     queryWrapper.eq("sort" ,pmsProductCategory.getSort());
}
    if(StringUtils.isNotBlank(pmsProductCategory.getIcon())){
     queryWrapper.eq("icon" ,pmsProductCategory.getIcon());
}
    if(StringUtils.isNotBlank(pmsProductCategory.getKeywords())){
     queryWrapper.eq("keywords" ,pmsProductCategory.getKeywords());
}
    if(StringUtils.isNotBlank(pmsProductCategory.getDescription())){
     queryWrapper.eq("description" ,pmsProductCategory.getDescription());
}
        queryWrapper.select("id","parent_id","name","create_name","level","product_unit","nav_status","icon","keywords","show_status");
        List<PmsProductCategory> list = pmsProductCategoryService.list(queryWrapper);
        return AjaxResult.success(pmsProductCategoryService.buildDeptTree(list));
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('mall:category:export')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(PmsProductCategory pmsProductCategory) {
        QueryWrapper<PmsProductCategory> queryWrapper = new QueryWrapper<PmsProductCategory>(pmsProductCategory);
        List<PmsProductCategory> list = pmsProductCategoryService.list(queryWrapper);
        ExcelUtil<PmsProductCategory> util = new ExcelUtil<PmsProductCategory>(PmsProductCategory. class);
        return util.exportExcel(list, "category" );
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:category:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(pmsProductCategoryService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('mall:category:add')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsProductCategory pmsProductCategory) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        pmsProductCategory.setCreateName(loginUser.getUsername());
        pmsProductCategory.setCreateById(loginUser.getUser().getUserId());
        return toAjax(pmsProductCategoryService.save(pmsProductCategory) ? 1 : 0);
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('mall:category:edit')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsProductCategory pmsProductCategory) {
        return toAjax(pmsProductCategoryService.updateById(pmsProductCategory) ? 1 : 0);
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('mall:category:remove')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsProductCategoryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    @GetMapping("/treeselect")
    public AjaxResult treeselect(PmsProductCategory pmsProductCategory)
    {
        /*获取当前用户*/
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        QueryWrapper<PmsProductCategory> queryWrapper = new QueryWrapper<PmsProductCategory>(pmsProductCategory);
//        queryWrapper.select("id","parent_id","name","create_name","level");
//
//        queryWrapper.eq("create_by_id",loginUser.getUser().getUserId());
        List<PmsProductCategory> depts = pmsProductCategoryService.list(pmsProductCategory);
        return AjaxResult.success(pmsProductCategoryService.buildDeptTreeSelect(depts));
    }
}
